package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.ProgramaService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.ProgramaVO;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class OtorgarBeneficiosAction extends Action {
	@Autowired
	ProgramaService programaServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String strCode = request.getParameter("code");
		// String strUsuario = request.getParameter("Usuario");

		Object codePrograma = (Object) request.getParameter("codePrograma");

		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");

		session.setAttribute("beneficiario", strCode); // OJO ESTA LINEA ES LA
														// QUE HAY QUE DEJAR
		// session.setAttribute("beneficiario","46"); // LINEA DE PRUEBA

		session.setAttribute("Usuario", user.getCode().toString());
		// session.setAttribute("Usuario","51"); // LINEA DE PRUEBA

		Collection<ProgramaVO> programas = null;
		Collection<SubprogramaVO> subprogramas = null;

		ProgramaVO objProgramaVO = new ProgramaVO();
		SubprogramaVO objSubProgramaVO = new SubprogramaVO();

		if (codePrograma == null) {
			session.removeAttribute("programas");
			try {
				programas = programaServiceImpl.findProgramaByCriteriaOB(
						objProgramaVO, user.getCode(), user.getUserGroupName(),
						user.getCodeArea());
			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}
			session.removeAttribute("msg");
			if (programas.size() > 0) {

				session.setAttribute("programas", programas);
			} else {
				session.setAttribute("msg",
						"No se encontraron programas vigentes a la fecha..");
			}
		}

		if (codePrograma != null) {
			session.removeAttribute("subprogramas");
			String codpro = (String) codePrograma;
			if (!codpro.equals("nulo")) {
				try {
					objSubProgramaVO.setCodePrograma(new Long(
							(String) codePrograma));
					subprogramas = subprogramaServiceImpl
							.findSubprogramaByCriteria(objSubProgramaVO,
									user.getCode(), user.getUserGroupName(),
									user.getCodeArea());
				} catch (DAOException e) {
					SUBException.logDebugError("", e);
				}
				session.removeAttribute("msg");
				if (subprogramas.size() > 0) {
					session.setAttribute("subprogramas", subprogramas);
				} else {
					session.setAttribute("msg",
							"No se encontraron subprogramas asociados a este programa que esten vigentes..");
				}
			} else
				session.setAttribute("msg", "");
		}

		return mapping.findForward("success");
	}
}